package jt.classic.system.objectquery;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.workflow.WObject;
import jt.classic.system.workflow.WObjectRegister;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.Column;
import org.limp.basework.SimpleBean;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.StringTool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 对象查询管理action
 *
 * @author mido
 *
 */
public class ObjectQueryAction extends SimpleAction {
    /**
     * 查询项设置列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward setlist(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        String obj = StringTool.checkString(request.getParameter("obj"));

        String type = StringTool.checkString(request.getParameter("type"));

        WObjectRegister register = WObjectRegister.getInstance();
        WObject object = register.findWObject(obj);

        List list = new ArrayList();

        List tlist = object.itables();

        for (int i = 0; i < tlist.size(); i++) {
            /*流程对象关联表*/
            Table table = (Table) tlist.get(i);

            List columns = table.columns();

            for (int j = 0; j < columns.size(); j++) {
                /*表字段*/
                Column column = (Column) columns.get(j);

                if (!column.isAnnex() && !column.isLob() &&
                        !column.isPremaryKey()) {
                    ObjectQueryItemJsonBean bean = new ObjectQueryItemJsonBean();
                    bean.setName(column.getDescription());
                    bean.setColumn(column.getName());
                    bean.setColumnName(column.getDescription());
                    bean.setItemType(0);
                    bean.setMode("");
                    bean.setParam("");
                    bean.setTable(table.getName());
                    bean.setTableName(table.getDescription());
                    bean.setType("");
                    bean.setColumnType(column.getType().toString());
                    bean.setTableHandleType((object.handleTable(table) == 0)
                        ? "单数据处理" : "多数据处理");

                    list.add(bean);
                }
            }
        }

        JSONArray jsonArray = JSONArray.fromObject(list);

        String jsonString = "{\"totalCount\":\"" + list.size() +
            "\",\"queryList\":";
        jsonString += jsonArray.toString();
        jsonString += "}";

        request.setAttribute("result", jsonString);

        return mapping.findForward("jsonresult");
    }

    /**
     * 通用查询列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ObjectQueryRegister register = ObjectQueryRegister.getInstance();

        List list = register.list();

        for (int i = 0; i < list.size(); i++) {
            ObjectQuery query = (ObjectQuery) list.get(i);

            ObjectQueryJsonBean queryBean = new ObjectQueryJsonBean();
            queryBean.setKey(query.id());
            queryBean.setDesc(query.idesc());
            queryBean.setOname(query.iobject().iname());
            queryBean.setObject(query.iobject().ikey());
            queryBean.setEdate(query.icreatestime());
            queryBean.setEdate(query.icreateetime());
            queryBean.setName(query.iname());
            queryBean.setPurview(query.ipurview());
            queryBean.setPurviewName(query.ipurviewname());
            queryBean.setSession(query.isession());

            list.set(i, queryBean);
        }

        JSONArray jsonArray = JSONArray.fromObject(list);

        String jsonString = "{\"totalCount\":\"" + list.size() +
            "\",\"queryList\":";
        jsonString += jsonArray.toString();
        jsonString += "}";

        request.setAttribute("result", jsonString);

        return mapping.findForward("jsonresult");
    }

    /**
     * 编辑对象查询设置
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        SimpleBean cb = new CommonBean4HSQ(request);

        Map param = cb.getResource();

        String key = StringTool.checkString(param.get("key"));

        ObjectQueryRegister register = ObjectQueryRegister.getInstance();

        ObjectQuery query = register.get(key);

        request.setAttribute("query", query);

        return mapping.findForward("edit4ext");
    }

    /**
     * 保存对象查询设置
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        SimpleBean cb = new CommonBean4HSQ(request);

        Map param = cb.getResource();

        String qkey = StringTool.checkString(param.get("qkey"));

        String xmlscript = StringTool.checkString(param.get("xmlscript"));

        ObjectQueryRegister register = ObjectQueryRegister.getInstance();

        if (register.contains(qkey)) {
            request.setAttribute("result", "{success: false, result : 0}");
        } else {
            try {
                register.add(xmlscript);

                request.setAttribute("result", "{success: true}");
            } catch (Exception ex) {
                request.setAttribute("result", "{success: false, result : -1}");
                ISystem.catchException(request, this, ex);
            }
        }

        return mapping.findForward("result");
    }
    
    /**
     * 修改对象查询设置
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward update(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        SimpleBean cb = new CommonBean4HSQ(request);

        Map param = cb.getResource();

        String qkey = StringTool.checkString(param.get("qkey"));
        
        /*原唯一标识*/
        String qkey1 = StringTool.checkString(param.get("qkey1"));

        String xmlscript = StringTool.checkString(param.get("xmlscript"));

        ObjectQueryRegister register = ObjectQueryRegister.getInstance();

        if (!qkey.equals(qkey1) && register.contains(qkey)) {
            request.setAttribute("result", "{success: false, result : 0}");
        } else {
            try {
                register.update(qkey, xmlscript);

                request.setAttribute("result", "{success: true}");
            } catch (Exception ex) {
                request.setAttribute("result", "{success: false, result : -1}");
                ISystem.catchException(request, this, ex);
            }
        }

        return mapping.findForward("result");
    }

    /**
     * 删除对象查询设置
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        SimpleBean cb = new CommonBean4HSQ(request);

        Map param = cb.getResource();

        String ids = StringTool.checkString(param.get("ids"));

        if (!ids.equals("")) {
            String[] idstr = ids.split(",");

            ObjectQueryRegister register = ObjectQueryRegister.getInstance();

            try {
                for (int i = 0; i < idstr.length; i++) {
                    if (!idstr[i].equals("")) {
                        register.remove(idstr[i]);
                    }
                }

                request.setAttribute("result", "{success: true}");
            } catch (Exception ex) {
                request.setAttribute("result", "{success: false}");
                ISystem.catchException(request, this, ex);
            }
        }

        return mapping.findForward("result");
    }
}
